#region File Description
//-----------------------------------------------------------------------------
// Cursor.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace _3D_FileExplorer
{
    /// <summary>
    /// Cursor is a DrawableGameComponent that draws a cursor on the screen. It works
    /// differently on Xbox and Windows. On windows, this will be a cursor that is
    /// controlled using both the mouse and the gamepad. On Xbox, the cursor will be
    /// controlled using only the gamepad.
    /// </summary>
    public class Cursor : DrawableGameComponent
    {
        #region Constants

        // this constant controls how fast the gamepad moves the cursor. this constant
        // is in pixels per second.
        const float CursorSpeed = 250f;

        #endregion

        #region Fields and properties

        // the content manager is passed in in Cursor's constructor, and will be used to
        // load the texture for the cursor to draw with.
        ContentManager content;

        // this spritebatch is created internally, and is used to draw the cursor.
        SpriteBatch spriteBatch;
        
        // this is the sprite that is drawn at the current cursor position.
        // textureCenter is used to center the sprite when drawing.
        Texture2D cursorTexture;
        Vector2 textureCenter;

        // Position is the cursor position, and is in screen space. 
        private Vector2 position;
        public Vector2 Position
        {
            get { return position;}
        }
        public static Ray cursorRay;

        #endregion

        #region Creation and initialization
        
        // this constructor doesn't really do much of anything, just calls the base 
        // constructor, and saves the contentmanager so it can be used in
        // LoadContent.
        public Cursor(Game game, ContentManager content)
            : base(game)
        {
            this.content = content;
        }

        // LoadContent needs to load the cursor texture and find its center.
        // also, we need to create a SpriteBatch.
        protected override void LoadContent()
        {
         

            base.LoadContent();
        }

        #endregion
                
        #region Draw

        // Draw is pretty straightforward: we'll Begin the SpriteBatch, Draw the cursor,
        // and then End.
        public override void Draw(GameTime gameTime)
        {
            

            base.Draw(gameTime);
        }

        #endregion

        #region Update

        // Update gets the current gamepad state and mouse state and uses that data to
        // calculate where the cursor's position is on the screen. On xbox, the position
        // is clamped to the viewport so that the cursor can't go off the screen. On
        // windows, doing something like that would be rude :)
        public override void Update(GameTime gameTime)
        {
            if (gameTime == null)
            
            throw new ArgumentNullException("gameTime");
            if (Game.IsActive)
            {
                cursorRay = CalculateCursorRay(Fe.camera.projection, Fe.camera.view, ((Fe)Game).GraphicsDevice);
            }


            position.X = Mouse.GetState().X;
            position.Y = Mouse.GetState().Y;

             
            base.Update(gameTime);
        }

        #endregion

        // CalculateCursorRay Calculates a world space ray starting at the camera's
        // "eye" and pointing in the direction of the cursor. Viewport.Unproject is used
        // to accomplish this. see the accompanying documentation for more explanation
        // of the math behind this function.
        public Ray CalculateCursorRay(Matrix projectionMatrix, Matrix viewMatrix,GraphicsDevice Device)
        {
            // create 2 positions in screenspace using the cursor position. 0 is as
            // close as possible to the camera, 1 is as far away as possible.
            Vector3 nearSource = new Vector3(Position, 0f);
            Vector3 farSource = new Vector3(Position, 1f);

            // use Viewport.Unproject to tell what those two screen space positions
            // would be in world space. we'll need the projection matrix and view
            // matrix, which we have saved as member variables. We also need a world
            // matrix, which can just be identity.
            Vector3 nearPoint = Device.Viewport.Unproject(nearSource,
                projectionMatrix, viewMatrix, Matrix.Identity);

            Vector3 farPoint = Device.Viewport.Unproject(farSource,
                projectionMatrix, viewMatrix, Matrix.Identity);

            // find the direction vector that goes from the nearPoint to the farPoint
            // and normalize it....
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            // and then create a new ray using nearPoint as the source.
            return new Ray(nearPoint, direction);
        }
    }
}
